package com.zerui.manager.accessrecord.gatewayimpl;


import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.cola.dto.Response;
import com.zerui.manager.accessrecord.AccessRecord;
import com.zerui.manager.accessrecord.convertor.AccessRecordConvert;
import com.zerui.manager.accessrecord.entity.AccessRecordDo;
import com.zerui.manager.accessrecord.entity.AccessRecordExport;
import com.zerui.manager.accessrecord.gateway.AccessRecordGateWay;
import com.zerui.manager.accessrecord.mapper.AccessRecordMapper;
import com.zerui.manager.common.util.EasyPoiUtil;
import com.zerui.manager.web.cmd.AccessRecordQryCmd;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author wh
 * @create_time 2023-08-23 15:28
 */
@Component
public class AccessRecordGateWayImpl implements AccessRecordGateWay {

    @Resource
    private AccessRecordMapper accessRecordMapper;

    @Override
    public Page<AccessRecord> getPageList(AccessRecordQryCmd cmd) {
		LambdaQueryWrapper<AccessRecordDo> queryWrapper = getAccessRecordDoLambdaQueryWrapper(cmd);
		Page<AccessRecordDo> page = new Page<>(cmd.getPageIndex(),cmd.getPageSize());
	    Page<AccessRecordDo> accessRecordDos = accessRecordMapper.selectPage(page, queryWrapper);
	    return AccessRecordConvert.INSTANCE.covertAccessRecordDoPageToAccessRecord(accessRecordDos);
    }

	@Override
	public void getAccessRecordListExport(AccessRecordQryCmd cmd, HttpServletResponse response) {
		LambdaQueryWrapper<AccessRecordDo> queryWrapper = getAccessRecordDoLambdaQueryWrapper(cmd);
		Integer count = accessRecordMapper.selectCount(queryWrapper);
		if(count > 50000){
			throw new BizException("本次数据导出超出最大限制5万条，请修改导出条件");
		}

		List<AccessRecordDo> accessRecords = accessRecordMapper.selectList(queryWrapper);
		String sheetName = "门禁通行记录列表导出";
		List<AccessRecordExport> accessRecordExports = AccessRecordConvert.INSTANCE.covertAccessRecordDoToAccessRecordExport(accessRecords);
		EasyPoiUtil.exportExcel(accessRecordExports, sheetName, sheetName, AccessRecordExport.class,
				sheetName + ".csv", response);
	}

	@Override
	public Response addAccessRecord(AccessRecord accessRecord) {
		AccessRecordDo accessRecordDo = AccessRecordConvert.INSTANCE.covertAccessRecordToAccessRecordDo(accessRecord);
		accessRecordMapper.insert(accessRecordDo);
		return Response.buildSuccess();
	}

	private LambdaQueryWrapper<AccessRecordDo> getAccessRecordDoLambdaQueryWrapper(AccessRecordQryCmd cmd) {
		LambdaQueryWrapper<AccessRecordDo> queryWrapper = new LambdaQueryWrapper<>();
		if (ObjectUtils.isNotEmpty(cmd.getStartTime())) {
			queryWrapper.gt(AccessRecordDo::getCreateTime, cmd.getStartTime());
		}
		if (ObjectUtils.isNotEmpty(cmd.getEndTime())) {
			queryWrapper.lt(AccessRecordDo::getCreateTime, cmd.getEndTime());
		}
		queryWrapper.like(StringUtils.isNotBlank(cmd.getPosition()), AccessRecordDo::getPosition, cmd.getPosition());
		queryWrapper.like(StringUtils.isNotBlank(cmd.getPersonnelName()), AccessRecordDo::getPersonnelName, cmd.getPersonnelName());
		queryWrapper.eq(StringUtils.isNotBlank(cmd.getPersonType()), AccessRecordDo::getPersonType, cmd.getPersonType());
		queryWrapper.eq(ObjectUtils.isNotEmpty(cmd.getOrgId()), AccessRecordDo::getOrgId, cmd.getOrgId());
		queryWrapper.like(StringUtils.isNotBlank(cmd.getDeviceName()), AccessRecordDo::getDeviceName, cmd.getDeviceName());
		queryWrapper.like(StringUtils.isNotBlank(cmd.getDeviceCode()), AccessRecordDo::getDeviceCode, cmd.getDeviceCode());
		queryWrapper.eq(ObjectUtils.isNotEmpty(cmd.getPassStatus()), AccessRecordDo::getPassStatus, cmd.getPassStatus());
		queryWrapper.eq(ObjectUtils.isNotEmpty(cmd.getAccess()), AccessRecordDo::getAccess, cmd.getAccess());
		queryWrapper.eq(ObjectUtils.isNotEmpty(cmd.getMode()), AccessRecordDo::getMode, cmd.getMode());
		queryWrapper.orderByDesc(AccessRecordDo::getCreateTime);
		return queryWrapper;
	}
}
